/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.cstLinkman.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.cstLinkman.domain.CstLinkman;
import qs.crm.modules.client.cstLinkman.repository.CstLinkmanRepository;
import qs.crm.modules.client.cstLinkman.service.CstLinkmanService;
import qs.crm.modules.client.cstLinkman.service.dto.CstLinkmanDto;
import qs.crm.modules.client.cstLinkman.service.dto.CstLinkmanQueryCriteria;
import qs.crm.modules.client.cstLinkman.service.mapstruct.CstLinkmanMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-01
**/
@Service
@RequiredArgsConstructor
public class CstLinkmanServiceImpl implements CstLinkmanService {

    private final CstLinkmanRepository cstLinkmanRepository;
    private final CstLinkmanMapper cstLinkmanMapper;

    @Override
    public Map<String,Object> queryAll(CstLinkmanQueryCriteria criteria, Pageable pageable){
        Page<CstLinkman> page = cstLinkmanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(cstLinkmanMapper::toDto));
    }

    @Override
    public List<CstLinkmanDto> queryAll(CstLinkmanQueryCriteria criteria){
        return cstLinkmanMapper.toDto(cstLinkmanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CstLinkmanDto findById(Long lkmId) {
        CstLinkman cstLinkman = cstLinkmanRepository.findById(lkmId).orElseGet(CstLinkman::new);
        ValidationUtil.isNull(cstLinkman.getLkmId(),"CstLinkman","lkmId",lkmId);
        return cstLinkmanMapper.toDto(cstLinkman);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CstLinkmanDto create(CstLinkman resources) {
        return cstLinkmanMapper.toDto(cstLinkmanRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CstLinkman resources) {
        CstLinkman cstLinkman = cstLinkmanRepository.findById(resources.getLkmId()).orElseGet(CstLinkman::new);
        ValidationUtil.isNull( cstLinkman.getLkmId(),"CstLinkman","id",resources.getLkmId());
        cstLinkman.copy(resources);
        cstLinkmanRepository.save(cstLinkman);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long lkmId : ids) {
            cstLinkmanRepository.deleteById(lkmId);
        }
    }

    @Override
    public void download(List<CstLinkmanDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CstLinkmanDto cstLinkman : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户编号", cstLinkman.getLkmCustNo());
            map.put("客户名称", cstLinkman.getLkmCustName());
            map.put("姓名", cstLinkman.getLkmName());
            map.put("性别 ", cstLinkman.getLkmSex());
            map.put("职位", cstLinkman.getLkmPostion());
            map.put("办公电话 ", cstLinkman.getLkmTel());
            map.put("手机", cstLinkman.getLkmMobile());
            map.put("备注", cstLinkman.getLkmMemo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}